<?php

/*
 * G�nere un calendrier visible tout le temps.
 * Appelle une url sp�cifi�e (symfony) lors de changement de date
 * Les variables suivantes seront rajout�es dans l'url : df_day , df_year, df_month (entiers sans z�ro)
 * Attention, les mois sont numerotes � partir de zero. Pourquoi ? J'en sais rien.
 * 
 * @param string url symfony � atteindre en cas de changement de date
 * @options string options � transmettre. ex : " calendar_options = ' firstDay : 1  , date : \"05/15/2008\" '"
 * @return string XHTML compliant  g�n�re un div + un script 
 */

function div_flat_cal_to_url($url, $options = array()) {
  $uniqFunctionName = 'changeDateSemaine' . rand(1, 1000); // TODO: remplacer cet id par un hash g�n�r� par l'url
  $prepUrl = url_for($url);

  if (preg_match('/uf:([1-9]+)/', $options, $matches)) {
    $options_url  = "'/'+uf+'/'+d+'/'+m+'/'+y";
    $uf           = 'var uf = ' . $matches[1];
  } else {
    $options_url  = "'/'+d+'/'+m+'/'+y";
    $uf           = '';
  }

  $js = " <script type='text/javascript'>
		function $uniqFunctionName(calendar) { 
		if (calendar.dateClicked) {
      		// OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
          $uf
      		var y = calendar.date.getFullYear();
      		var m = 1 + calendar.date.getMonth();     // integer, 1..12
      		var d = calendar.date.getDate();      // integer, 1..31
     		window.location.replace('$prepUrl'+$options_url );
    }}
	</script> ";
  return $js . div_flat_cal($uniqFunctionName, $options);
}

/*
 * G�nere un calendrier visible tout le temps.
 * Un changement de date appele une fonction javascript
 * 
 * <b> exemple d'appel </b>
 * print div_flat_cal('day_selected',''," calendar_options = ' firstDay : 1  , date : \"05/15/2008\" '") ;
 * 
 * <b>exemple de fonction javascript appel�e</b>
 * <code>
 * 	function day_selected(calendar) { 
  if (calendar.dateClicked) {
  // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
  var y = calendar.date.getFullYear();
  var m = 1 + calendar.date.getMonth();     // integer, 1 + ( 0..11 )
  var d = calendar.date.getDate();      // integer, 1..31
  var input_hidden_field = document.getElementById("'.$id_inputField.'");
  input_hidden_field.value =  y + "-" + m + "-" + d;
  }}
 * </code>
 * 
 *  */

function div_flat_cal($functionJavascript, $options = array()) {

  $options = _parse_attributes($options);
  $context = sfContext::getInstance();
  $culture = _get_option($options, 'culture', $context->getUser()->getCulture());
  $withTime = _get_option($options, 'withtime', false);

  $pattern = _get_option($options, 'format', $withTime ? 'g' : 'd');

  $dateFormat = new sfDateFormat($culture);

  $pattern = $dateFormat->getInputPattern($pattern);


  // register our javascripts and stylesheets
  $langFile = sfConfig::get('app_dir_sf_calendar_web_dir') . '/lang/calendar-' . strtolower(substr($culture, 0, 2));
  $jss = array(
      sfConfig::get('app_dir_sf_calendar_web_dir') . '/calendar',
      is_readable(sfConfig::get('sf_symfony_data_dir') . '/web/' . $langFile . '.js') || is_readable(sfConfig::get('sf_web_dir') . '/' . $langFile . '.js') ? $langFile : sfConfig::get('app_dir_sf_calendar_web_dir') . '/lang/calendar-en',
      sfConfig::get('app_dir_sf_calendar_web_dir') . '/calendar-setup',
  );
  foreach ($jss as $js) {
    $context->getResponse()->addJavascript($js);
  }

  // css
  if ($calendar_style = _get_option($options, 'css', 'skins/aqua/theme')) {
    $context->getResponse()->addStylesheet(sfConfig::get('app_dir_sf_calendar_web_dir') . '/' . $calendar_style);
  }

  // date format
  $date_format = $dateFormat->getPattern($pattern);

  // calendar date format
  $calendar_date_format = $date_format;
  $calendar_date_format = strtr($date_format, array('yyyy' => 'Y', 'yy' => 'y', 'MM' => 'm', 'M' => 'm', 'dd' => 'd', 'd' => 'e', 'HH' => 'H', 'H' => 'k', 'hh' => 'I', 'h' => 'l', 'mm' => 'M', 'ss' => 'S', 'a' => 'p'));

  $calendar_date_format = preg_replace('/([mdyhklspe])+/i', '%\\1', $calendar_date_format);

  $idCalContainer = get_id_from_name('container_' . $functionJavascript);
  $js = '

    Calendar.setup({
      flat : "' . $idCalContainer . '",
      flatCallback :' . $functionJavascript . ' ';

  if ($withTime) {
    $js .= ",\n showsTime : true";
  }

  // calendar options
  if ($calendar_options = _get_option($options, 'calendar_options')) {
    $js .= ",\n" . $calendar_options;
  }

  $js .= '
    });
  ';


  // construct html
  if (!isset($options['size'])) {
    // educated guess about the size
    $options['size'] = strlen($date_format) + 2;
  }


  $html = tag('div', array('id' => $idCalContainer));
  if (_get_option($options, 'with_format')) {
    $html .= '(' . $date_format . ')';
  }
  // add javascript
  $html .= content_tag('script', $js, array('type' => 'text/javascript'));

  return $html;
}

?>